package com.example.demo.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;

/**
 * ScheduleConfig 可设置多个，针对不同可灵活修改定时规则的任务（一般的定时任务只用@Scheduled+@Async）
 **/
@Data
@Slf4j
@Component
public class ScheduleConfig implements SchedulingConfigurer {
    /*private String cron;
    private Long timer;

    private final ApplicationContext context;

    public ScheduleConfig(ApplicationContext context) {
        cron = "0/5 * * * * ?";
        timer = 10000L;

        this.context = context;
    }*/

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        /*taskRegistrar.setScheduler(context.getBean("sendEmailScheduler"));
        taskRegistrar.addTriggerTask(
                () -> {
                    log.info("config1:当前时间：{}", LocalDateTime.now());
                    log.info("config1:线程名字：{}", Thread.currentThread().getName());
                },
                triggerContext -> {
                    if (cron != null && !cron.isEmpty()) {
                        CronTrigger cronTrigger = new CronTrigger(cron);
                        return cronTrigger.nextExecutionTime(triggerContext);
                    }
                    else {
                        PeriodicTrigger periodicTrigger = new PeriodicTrigger(timer);
                        return periodicTrigger.nextExecutionTime(triggerContext);
                    }
                }
        );*/
    }
}
